package pers.supeijin.devtools.converter.wx2vue.converter.template;

import com.intellij.openapi.util.Pair;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;


/**
 * @author 类选择器属性转工具
 * @date 2021/1/27.
 */
public class ClassAttributeUtils {
	public static final String CLASS_TAG = "class";
	public static final String BIND_CLASS_TAG = ":class";

	private ClassAttributeUtils() {
	}
	/**
	 * 转换class 选择器属性
	 * 原始(小程序) class="confirm-button {{isHighlight?'highlight-bg':'default-bg'}}"
	 * 转换后(Vue)  class="confirm-button"  :class="isHighlight?'highlight-bg':'default-bg'"
	 * @param xmlTag
	 */
	public static void converterClassAttributes(XmlTag xmlTag) {
		XmlAttribute xmlAttribute = xmlTag.getAttribute(CLASS_TAG);
		if(xmlAttribute!=null){
			String value = xmlTag.getAttribute(CLASS_TAG).getValue();
			Pair<Integer, Integer> converterInfo =BindUtils.getBindInfo(value);
			if(converterInfo!=null){
				String bindStr = value.substring(converterInfo.first,converterInfo.second+2);
				String oldText= value.replace(bindStr,"");
				if(oldText.trim().length()>0){
					xmlAttribute.setValue(oldText);
				}else{
					xmlAttribute.delete();
				}
				xmlTag.setAttribute(BIND_CLASS_TAG, bindStr.substring(2,bindStr.length()-2));
			}
		}
	}
}
